package com.opennews.openplatform


import static org.springframework.http.HttpStatus.OK

class FileController extends BaseController {
    static allowedMethods = [
        uploadOwnAvatarByBase64: "POST",
        uploadOwnAvatar: "POST",
        uploadAvatar: "POST",
        upload: "POST"
    ]

    FileService fileService

    def index() {
        final String filename = "book.csv"

        def outputStream = response.outputStream
        response.setContentType("application/csv")
        response.setHeader("Content-Disposition", "attachment; filename=${filename}")
        response.status = OK.value()

        outputStream << "This is testing"

        outputStream.flush()
        outputStream.close()
    }

    /**
     * This is an example of file export.
     */
    def download() {
        final String filename = "book.csv"

        def outputStream = response.outputStream
        response.setContentType("application/csv")
        response.setHeader("Content-Disposition", "attachment; filename=${filename}")
        response.status = OK.value()

        outputStream << "This is testing"

        outputStream.flush()
        outputStream.close()
    }

    def uploadOwnAvatarByBase64() {
        def requestBodyData = getRequestBodyData()

        // Gets current user's id to update the avatar info.
        def userId = getClaims().id

        // Uploads the avatar file and updates the user avatar info.
        def avatarUrl = fileService.uploadAvatarByBase64(userId, requestBodyData.filename.toString(), requestBodyData.base64Content.toString())

        // Sends the avatar url back to client.
        sendDataResponse(avatarUrl)
    }

    def uploadOwnAvatar() {
        // IMPORTANT: The value of the getFiles method parameter must be the same of the file uploader's html name.
        def files = request.getFiles("files")

        // Gets current user's id to update the avatar info.
        def userId = getClaims().id

        // Uploads the avatar file and updates the user avatar info.
        def avatarUrl = fileService.uploadAvatar(userId, files.first())

        // Sends the avatar url back to client.
        sendDataResponse(avatarUrl)
    }

    def uploadAvatar() {
        // IMPORTANT: The value of the getFiles method parameter must be the same of the file uploader's html name.
        def files = request.getFiles("files")

        // Gets the user's id to update the avatar info.
        def userId = params.userId.toString()

        // Uploads the avatar file and updates the user avatar info.
        def avatarUrl = fileService.uploadAvatar(userId, files.first())

        // Sends the avatar url back to client.
        sendDataResponse(avatarUrl)
    }

    def upload() {
        // IMPORTANT: The value of the getFiles method parameter must be the same of the file uploader's html name.
        def files = request.getFiles("files")
        def id = request.getParameter("id")

        def newFilenames = fileService.uploadFiles(files)

        // Sends the temp file paths back to client. It can use it to associate with the form data.
        sendDataResponse(newFilenames)
    }
}